admin.controller('AdminController', function($scope, $http,$location,$rootScope,$timeout, authService) {
	if(!authService.checkLogin())
		$location.path('/login');
		
	$scope.logout = function(){
		authService.logout();
		$location.path("/login");
	}
});
admin.controller('LoginController', function($scope, $http,$location,$rootScope,$timeout,authService) {
	$scope.busy = false;
	$scope.login= function(){
		$scope.busy = true;
		$http.post( '/api/admin/login', {
			username: $scope.username,
			password: $scope.password
		}).success(function(res){
			if(res.error){
				$scope.errorMsg = res.error;
			} else {
				authService.setToken(res.token);
				$location.path('/category');
			}
			$timeout(function(){
				$scope.busy = false;
			},3000)
			
		});
	}
});
admin.controller('CategoryController', function($scope, $http,$location,$rootScope,$timeout) {
	$scope.msg = '';
	$scope.total1= 0;
	$scope.total2= 0;
	$scope.limit = 5;
	$scope.curPage1 = 1;
	$scope.curPage2 = 1;
	$scope.getCatList = function(){
		$http.get( '/api/admin/category?page='+$scope.curPage1+'&limit='+$scope.limit
		).success(function(res){
			$scope.total1 = res.total;
			$scope.catList = res.data;
		});
	}
	$scope.getAllCatList = function(){
		$http.get( '/api/admin/category?page=1&limit=1000'
		).success(function(res){
			$scope.catTotalList = res.data;
		});
	}
	
	$scope.getSubCatList = function(){
		$http.get( '/api/admin/subCategory?page='+$scope.curPage2+'&limit='+$scope.limit
		).success(function(res){
			$scope.total2 = res.total;
			$scope.catSubList = res.data;
		});
	}
	$scope.addCategory = function() {
		$http.post('/api/admin/category',{
			name: $scope.catName
		}).success(function(res){
			$scope.catName = "";
			$scope.status = res.status;
			$scope.msg = res.msg;
			$timeout(function(){
				$scope.msg = '';
			},2000);
			$scope.getCatList();
			$scope.getAllCatList();
		})
	}
	
	$scope.addSubCategory = function() {
		$http.post('/api/admin/subCategory',{
			name: $scope.catSubName,
			catId: $scope.selectCat
		}).success(function(res){
			$scope.catSubName = "";
			$scope.status = res.status;
			$scope.msg = res.msg;
			$timeout(function(){
				$scope.msg = '';
			},2000);
			$scope.getSubCatList();
		})
	}
	$scope.updateCatName = function(data, id){
		$http.put( '/api/admin/category',{
			catId: id,
			name: data
		}).success(function(res){
			$scope.status = res.status;
			$scope.msg = res.msg;
			$timeout(function(){
				$scope.msg = '';
			},2000);
			if(!res.status)
				$scope.getCatList();
			$scope.getAllCatList();
			$scope.getSubCatList();
		});
	}
	
	$scope.updateSubCatName = function(data, id){
		$http.put( '/api/admin/subCategory',{
			catSubId: id,
			name: data
		}).success(function(res){
			$scope.status = res.status;
			$scope.msg = res.msg;
			$timeout(function(){
				$scope.msg = '';
			},2000);
			if(!res.status)
				$scope.getSubCatList();
		});
	}
	
	$scope.deleteCat = function(index, id){
		if(confirm("Bạn chắc chắn muốn xóa danh mục này?")){
			$http.delete('/api/admin/category?id='+id)
			.success(function(res){
				$scope.getCatList();
				$scope.getSubCatList();
				$scope.getAllCatList();
			})
		}
	}
	$scope.deleteSubCat = function(index, id){
		if(confirm("Bạn chắn chắc muốn xóa danh mục con này?")){
			$http.delete('/api/admin/subCategory?id='+id)
			.success(function(res){
				$scope.getSubCatList();
			})
		}
	}
});

admin.controller('PasswordController', function($scope, $http,$location,$rootScope,$timeout,authService) {
	$scope.msg = '';
	
	$scope.changePassword = function(){
		if($scope.newPassword != $scope.reNewPassword){
			$scope.status = false;
			$scope.msg= "Mật khẩu xác nhận không đúng";
			$timeout(function(){
				$scope.msg="";
			},2000);
		}else{
			$http.put( '/api/admin/password', {
				username: "admin",
				oldPassword: $scope.oldPassword,
				newPassword: $scope.newPassword
			}).success(function(res){
				$scope.oldPassword = "";
				$scope.newPassword = "";
				$scope.reNewPassword = "";
				$scope.status = res.status;
				$scope.msg= res.msg;
			});
		}
	}
});

admin.controller('ProductController', function($scope, $http, $location,$rootScope,$timeout,authService, $modal) {
	$scope.msg = '';
	$scope.total= 0;
	$scope.limit = 10;
	$scope.curPage = 1;
	
	$scope.getProductList = function(){
		$http.get( '/api/admin/product?page='+$scope.curPage+'&limit='+$scope.limit
		).success(function(res){
			$scope.total = res.total;
			$scope.productList = res.data;
		});
	}
	$scope.deleteProduct = function(id){
		if(confirm("Bạn chắc chắn muốn xóa sản phẩm này?")){
			$http.delete('/api/admin/product?id='+id)
			.success(function(res){
				$scope.getProductList();
			})
		}
	}
	$scope.openCreatePopup = function(){
		var modalInstance = $modal.open({
			templateUrl: 'product-popup.html',
			controller: 'AddProductController',
			backdrop: 'static',
			size: 'lg'
		});
		modalInstance.result.then(function () {
			console.log('save');
			$scope.getProductList();
		});
	}
});

admin.controller('AddProductController', function($scope, $http, $modalInstance, $timeout ) {
	$scope.imageList =[];
	var hasUpload = [];
	$scope.urlList =[];
	$scope.uploadStatus = false;
	$scope.ok = function () {
		
		$scope.uploadStatus = true;
		angular.forEach($scope.imageList, function(value, key){
			var fd = new FormData();
			fd.append('file', value);
			$http.post(
				'/api/upload', 
				fd, {
				transformRequest: angular.identity,
				headers: {
					"Content-Type": undefined
				},
			}).success(function (data) {
				if(data.status){
					hasUpload.push(data.name);
					if(key==$scope.imageList.length-1){
						console.log(hasUpload);
						$http.post('/api/admin/product',{
							name: $scope.productName,
							catId: $scope.selectCat,
							subId: $scope.selectSubCat,
							price: $scope.price,
							desc: $scope.description,
							image1: hasUpload[0],
							image2: hasUpload[1],
							image3: hasUpload[2]
						}).success(function(res){
							if(res.status){
								$scope.uploadStatus = false;
								$modalInstance.close();
							}else
								$scope.uploadStatus = false;
						})
					}
				}else
					$scope.uploadStatus = false;
				
			});
		});
				
		// $modalInstance.close();
	};
	$scope.getAllCatList = function(){
		$http.get( '/api/admin/category?page=1&limit=1000'
		).success(function(res){
			$scope.catTotalList = res.data;
		});
	}
	$scope.getSubCatById = function(){
		$http.get( '/api/admin/subCategoryById?id='+$scope.selectCat
		).success(function(res){
			$scope.subCatList = res.data;
		});
	}
	
	$scope.onFileSelect = function($files) {
		angular.forEach($files, function(value, key){
			if (value.type.indexOf('image') > -1 ) {
				$scope.imageList.push(value);
				var reader = new FileReader();
				reader.onload = function(e) {
					$scope.urlList.push(e.target.result);
					$scope.$apply();
				}
				reader.readAsDataURL(value);
			}
		});	
	};
	$scope.removePhoto = function(index){
		$scope.imageList.splice(index,1);
		$scope.urlList.splice(index,1);		
	}
	
	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};
});

admin.controller('OnlineSupportController', function($scope, $http,$location,$rootScope,$timeout) {
	$scope.msg = '';
	$scope.getOnlineSupportList = function(){
		$http.get( '/api/admin/onlineSupport'
		).success(function(res){
			$scope.onlineSupportList = res.data;
		});
	}
	
	$scope.addOnlineSupport = function() {
		$http.post('/api/admin/onlineSupport',{
			name: $scope.supName,
			phone: $scope.supPhone,
			mail: $scope.supMail
		}).success(function(res){
			$scope.supName = "";
			$scope.supPhone = "";
			$scope.supMail = "";
			$scope.status = res.status;
			$scope.msg = res.msg;
			$timeout(function(){
				$scope.msg = '';
			},2000);
			$scope.getOnlineSupportList();
		})
	}
	
	$scope.updateOnlineSupport = function(data, id, type){
		$http.put( '/api/admin/onlineSupport',{
			id: id,
			name: data,
			type: type
		}).success(function(res){
			$scope.status = res.status;
			$scope.msg = res.msg;
			$timeout(function(){
				$scope.msg = '';
			},2000);
		});
	}
	
	$scope.deleteOnlineSupport = function(id){
		if(confirm("Bạn chắc chắn muốn xóa danh mục này?")){
			$http.delete('/api/admin/onlineSupport?id='+id)
			.success(function(res){
				$scope.getOnlineSupportList();
			})
		}
	}
});

admin.controller('GioithieuCtrl', function($scope, $http, $location,$rootScope,$timeout,authService) {
	$scope.customMenu = [
		['bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript'],
		['font'],
		['font-size'],
		['font-color', 'hilite-color'],
		['remove-format'],
		['ordered-list', 'unordered-list', 'outdent', 'indent'],
		['left-justify', 'center-justify', 'right-justify'],
		['code', 'quote', 'paragragh'],
		['link', 'image']
	];
	
	$scope.updateGioithieu = function(){
		$http.put( '/api/admin/updateGioithieu',{
			noidung: $scope.noidung
		}).success(function(res){
			if(!res.error){
				alert("Thay đổi thành công");
			}
		});
	}
	
	$scope.getGioithieu = function(){
		$http.get( '/api/admin/getGioithieu'
		).success(function(res){
			$scope.noidung = res.data.noidung;
		});
	}
	$scope.getGioithieu();
});